package com.yuantu.judiciary.utils;

import com.yuantu.judiciary.constant.Regulation;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ObjectUtils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * TimeUtil
 *
 * @author syw
 * @date 2020/7/3
 **/
@Slf4j
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class TimeUtil {

    /**
     * 根据指定的事件类型将字符串改为时间戳
     *
     * @param time     字符串时间
     * @param timeType 时间格式
     * @return timestamp
     */
    public static Timestamp transferTime(String time, String timeType) {
        if (StringUtils.isBlank(time)) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(timeType);
        Date date = new Date();
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            log.error("字符串转换时间戳失败：", e);
        }
        return new Timestamp(date.getTime());
    }

    /**
     * 根据指定的事件类型将字符串改为时间戳
     *
     * @param time     字符串时间
     * @param timeType 时间格式
     * @return timestamp
     */
    public static Date transferTimeDate(String time, String timeType) {
        if (ObjectUtils.isEmpty(time)) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(timeType);
        Date date = new Date();
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            log.error("字符串转换时间戳失败：", e);
        }
        return date;
    }

    public static Date transferTimeDate(Timestamp time, String timeType) {
        if (ObjectUtils.isEmpty(time)) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(timeType);
        Date date = new Date();
        try {
            date = sdf.parse(sdf.format(time));
        } catch (ParseException e) {
            log.error("字符串转换时间戳失败：", e);
        }
        return date;
    }

    /**
     * 根据指定的事件类型将字符串改为时间戳
     *
     * @param time     字符串时间
     * @param timeType 时间格式
     * @return timestamp
     */
    public static String transferTimeString(Timestamp time, String timeType) {
        if (ObjectUtils.isEmpty(time)) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(timeType);
        return sdf.format(time);
    }

    /**
     * 转换时间为一天的开始（用于统一时间）
     */
    public static Timestamp getDayBeginTime(Timestamp timestamp) {
        Date beginTime = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(Regulation.WHOLE_TIME);
        SimpleDateFormat startFormat = new SimpleDateFormat(Regulation.DAY_BEGIN);
        try {
            beginTime = dateFormat.parse(startFormat.format(timestamp));
        } catch (Exception e) {
            log.error("获取某天开始时间失败: ", e);
        }
        return new Timestamp(beginTime.getTime());
    }

    /**
     * 转换时间为一天的结束（用于统一时间）
     */
    public static Timestamp getDayEndTime(Timestamp timestamp) {
        Date endTime = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(Regulation.WHOLE_TIME);
        SimpleDateFormat endFormat = new SimpleDateFormat(Regulation.DAY_END);
        try {
            endTime = dateFormat.parse(endFormat.format(timestamp));
        } catch (Exception e) {
            log.error("获取某天结束时间失败：", e);
        }
        return new Timestamp(endTime.getTime());
    }

    /**
     * 获取两个日期之间的天数
     */
    public static int getDaysBetween(String smallDate, String bigDate) {
        try {
            // 日期格式
            SimpleDateFormat sdf = new SimpleDateFormat(Regulation.YEAR_MONTH_DAY);
            // 获取两个日期的时间戳
            Calendar cal = Calendar.getInstance();
            cal.setTime(sdf.parse(smallDate));
            long smallTime = cal.getTimeInMillis();
            cal.setTime(sdf.parse(bigDate));
            long bigTime = cal.getTimeInMillis();
            // 相差的日期
            long days = (bigTime - smallTime) / (1000 * 3600 * 24) + 1;
            // long转int 存在溢出情况  根据业务情况编辑catch中返回值
            return Integer.parseInt(String.valueOf(days));
        } catch (Exception e) {
            log.error("获取两个日期之间的天数失败：", e);
        }
        return 0;
    }

    /**
     * 获取两个时间节点之间的时间列表
     **/
    public static List<String> getMonthBetween(Date minDate, Date maxDate, String timeType, Integer type) {
        ArrayList<String> result = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat(timeType);

        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();
        min.setTime(minDate);
        max.setTime(maxDate);

        while (!min.after(max)) {
            result.add(sdf.format(min.getTime()));
            min.add(type, 1);
        }
        return result;
    }

}
